package com.italian.numbers;

import java.util.ArrayList;
import java.util.Random;

public class Expression {
	final int possibleVariants = 4;
	final int[] maxNumber = {12,20,50,100};
	final int[] minNumber = {2,2,13,13};
	final int maxNumber1 = 20;
	final int maxNumber2 = 100;
	final int maxNumber3 = 100;
	final int maxOperation0 = 1;
	final int maxOperation1 = 2;
	final int maxOperation2 = 2;
	final int maxOperation3 = 4;
	
	private int level = 0;
	String[] operation = {"+","-","*","/"};
	public Expression(){
		
	}
	public void setLevel(int level) {
		this.level = level;
	}
	public int getLevel() {
		return this.level;
			
	}
	/*
	 * runs expression generator with different parameters depending on current level
	 * @return - generated expression
	 */
	public ArrayList<String> getExpression(){
		if(this.level == 0)
			return this.getConcreteExpression(this.maxNumber[0],this.minNumber[0],this.maxOperation0);
		if(this.level == 1)
			return this.getConcreteExpression(this.maxNumber[1],this.minNumber[1],this.maxOperation1);
		if(this.level == 2)
			return this.getConcreteExpression(this.maxNumber[2],this.minNumber[2],this.maxOperation2);
		if(this.level == 3)
			return this.getConcreteExpression(this.maxNumber[3],this.minNumber[3],this.maxOperation3);
		return null;
	}
	
	/*
	 * calculates expression to show
	 * @param maxNumber - takes maximal value of generated number for expression
	 * @param maxOperation - takes maximal index of operation for expression
	 * @return - String[4] array with numbers and operation Example {"+","12","3","15","16","21","31"}, 4th element is correct answer
	 */
	private ArrayList<String> getConcreteExpression(int maxNumber, int minNumber,  int maxOperation){
		ArrayList<String> result = new ArrayList<String>();
		Integer a =0, b=0, c=0;
		Random rand = new Random();
		int operation = rand.nextInt(maxOperation);
		result.add(this.operation[operation]);
		/*
		 * generating result for "+" operation
		 */
		if(operation == 0){
			a = rand.nextInt(maxNumber-minNumber)+minNumber;
			b = rand.nextInt(maxNumber-a)+1;
			c = a+b;
		}
		if(operation == 1){
			a = rand.nextInt(maxNumber-minNumber)+minNumber;
			b = rand.nextInt(a-1)+1;
			c = a-b;
		}
		if(operation == 2){
			a = rand.nextInt((int) Math.round(Math.sqrt(maxNumber)-2))+2;
			b = rand.nextInt(maxNumber/a-1)+1;
			c = a*b;
		}
		if(operation == 3){
			c = rand.nextInt((int) Math.round(Math.sqrt(maxNumber))-2)+2;
			b = rand.nextInt(maxNumber/c-1)+1;
			a = c*b;
		}

		result.add(a.toString());
		result.add(b.toString());
		result.add(c.toString());

		for(int i=0;i<this.possibleVariants;i++){
			String possible = this.getRandomNumber(c, maxNumber);
			while(result.contains(possible)){
				possible = this.getRandomNumber(c, maxNumber);
			}
			result.add(possible);
		}
		return result;
	}
	
	/*
	 * @param number - number around which we are looking for possible values
	 * @param maxNumber - value to limit possible variants
	 * @return - String value of random number around parameter number
	 */
	private String getRandomNumber(Integer number, Integer maxNumber){
		Random rand = new Random();
		Integer b = rand.nextInt(2);
		Integer answer = 0;
		if(b==0){
			Integer help = rand.nextInt(number+1);
			answer = number - help;
		}
		else{
			Integer help = rand.nextInt(maxNumber-number+1);
			answer = number + help;
		}
		return answer.toString();
	}
	
}
